Title: BookStore
Objectives: Learning how to write a simple class. Learning how to use a
prewritten class. Understanding how object oriented design can aid program
design by making it easier to incorporate independently designed pieces of code
together into a single application.
Description:
In this homework, you will essentially write
a program that runs a simple bookstore. Three classes will be used in this homework.
1.
A
class called BookStore has been already implemented and you will get BookStore.java file from the course web page. You have to put this
file into the same directory with your other source files. You will just use the methods in BookStore class; you do not need to
know the content of this class. The available methods for this class are shown here.
2.
You
have to implement a class called Book as described below. The
name of this class must be Book and it must contain the instance methods below. Do not
change the names of the methods, because they are accessed from the BookStore class.
3.
You
have to also implement another class called MyBookStore. This class will contain
only a main method. In this main method, you will create a BookStore object first, and you will
perform certain menu-driven operations using this BookStore object.
Book class:
You are going to write a Book class. A Book object keeps track of
information for one particular book title that could potentially be stored in a
bookstore. Here are the instance variables for your class:
private String title;
private int numOfPages;
private double price;
private int quantity;
You have to include the following instance
methods in your Book class:
// Constructor: Takes in the
title of the book, the number of pages in the book, the cost of the book
// and the quantity of books
and initializes each of the appropriate instance variables in the object.
public Book(String theTitle,
int pages, double cost, int num)
// Returns the title of the
Book object the method is called on.
public String getTitle()
// Returns the price of the
Book object the method is called on.
public double getPrice()
// Returns the quantity of
the Book object the method is called on.
public int getQuantity()
// Returns all the
information about a Book object as a String. (Make it readable!)
public String toString()
// Subtracts the given
amount from the quantity in the Book object the method is called on.
public void
subtractQuantity(int num)
// Adds the given amount
from the quantity in the Book object the method is called on.
public void addQuantity(int
num)
BookStore class:
The BookStore class has been written for
you. You need to use the methods in BookStore class in order to create
your console application that will "run" a bookstore. In order to use
these methods you must download the BookStore.java file from the class web
page and save it in the same directory where your other source files (*.java)
reside. See here for the methods of this prewritten
class.
MyBookStore class:
You will create a console application that
will "run" a bookstore. Your MyBookStore class should contain only a
main method. In this main method, you will create a BookStore object first, and you will
perform certain menu-driven operations using this BookStore object. In order to use
these methods you must download the BookStore.java file from the class web
page. The main method of your MyBookStore class should do the
following:
Create a new BookStore object.
·
Prompt
the user with the following menu choices:
1) Add a book to the stock
2) Sell a book in stock
3) List the titles of all
the books in BookStore object
4) List all the information
about the books in BookStore object
5) Print out the gross
income of the bookstore
6) Quit
·
Execute
the choice the user chooses and then continue until they quit.
·
What
each menu choice should do:
1.
Adding
a book: Ask the user for the title of the book. If it is already in stock,
simply ask the user to enter how many extra books to stock, and then do so. If
not, ask the user for the rest of the information (number of pages, price, and
quantity) and add that book into the stock.
2.
Selling
a book: Ask the user for the title of the book they want to buy. If it is not
in stock, print a message to that effect. Otherwise, ask the user how many of
that book they want to buy. If there are enough copies of the book, make the
sale. If not, print out a message explaining why the transaction could not be
completed.
3.
List
the titles of all the books in BookStore object
4.
List
all the information about the books in BookStore object
5.
Print
out the gross income of the bookstore
6.
Quit.
Submittals:
Submittals should follow the specifications
given here.
Installation Instructions:
Below are brief instructions for installing BookStore.java on your machine. Note that BookStore.java cannot be compiled until Book.java has been written by
you. This is because the BookStore class makes use of the Book class. A quick note on the package issue: Your source files should NOT contain a
package statement since the BookStore.java file does not contain a package
statement. We want all three classes in
the same package.
Instructions for installing BookStore.java if you are using JDK
·
Write
Book.java (This file should NOT contain a package statement).
·
Download
BookStore.java (Do not alter this file
ever).
·
Copy
BookStore.java into the same directory that holds Book.java
·
Write
MyBookStore.java (This file should NOT contain a package statement).
Instructions for installing BookStore.java if you are using JBuilder
·
Create
new project. File > New Project…
Complete the wizard.
·
Create
Book class. File > New Class… This brings up a wizard.
o
Under
Class Information, make sure the Package field is blank.
o
The
Class name field should be Book.
·
Write
the Book class.
·
Download
BookStore.java (Do not alter this file
ever).
·
Copy
BookStore.java into the same directory that holds Book.java (Typically this is
C:\Documents and Settings\username\jbproject\projectname\src).
·
Add
BookStore.java to your project. (Project
> Add Files / Packages… Now search
for where you copied BookStore.java)
·
Create
MyBookStore class. File > New Class… This brings up a wizard.
o
Under
Class Information, make sure the Package field is blank.
o
The
Class name field should be MyBookStore.
·
Write
the MyBookStore class.
Prompting for user input using
a menu:
Check out this code
for tips on how to prompt for user input using a menu.